import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import pages.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class PostgreMybatis {

    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "postgre-mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        try(SqlSession openSession = sqlSessionFactory.openSession()) {
            UserMapper userMapper = openSession.getMapper(UserMapper.class);

            List<User> userList = new ArrayList<>();
            for (int i=0; i<10000; i++) {
                userList.add(new User((long)i, 13512341234L + i + "", "test" + i));
            }
            long start = System.currentTimeMillis();
            userMapper.postgreInsertBatch(userList);
            long end = System.currentTimeMillis();
            System.out.println("耗时" +(end - start)+ "毫秒");
        }

    }

}
